# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=opensubdiv
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.5.0
pkgrel=1
pkgdesc="An Open-Source subdivision surface library (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="http://graphics.pixar.com/opensubdiv"
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-tbb")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-glfw"
             "${MINGW_PACKAGE_PREFIX}-ptex"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-pygments"
             "${MINGW_PACKAGE_PREFIX}-python-docutils"
             "${MINGW_PACKAGE_PREFIX}-opencl-icd"
             "${MINGW_PACKAGE_PREFIX}-opencl-headers")
source=("https://github.com/PixarAnimationStudios/OpenSubdiv/archive/v${pkgver//./_}.tar.gz"
        001-support-tbb2021.patch::https://github.com/PixarAnimationStudios/OpenSubdiv/commit/9079a517.patch
        002-Fix-building-on-mingw-w64.patch)
sha256sums=('8f5044f453b94162755131f77c08069004f25306fd6dc2192b6d49889efb8095'
            '9d90d529330a5ddb74822ece2a1e0a33fa40f887c6de896e9daa987dbb5c18a4'
            '2de9d7a14fdca3aa3e34f15b4ad3c0a9db137a5b97a755a496a4bb703163426d')

prepare() {
  cd "${srcdir}"/"OpenSubdiv-${pkgver//./_}"

  patch -Np1 -i "${srcdir}"/001-support-tbb2021.patch # Support TBB 2021
  patch -Np1 -i "${srcdir}"/002-Fix-building-on-mingw-w64.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -w" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS=ON \
    -DNO_CLEW=ON \
    -DNO_CUDA=ON \
    -DNO_EXAMPLES=ON \
    -DNO_REGRESSION=ON \
    -DNO_TUTORIALS=ON \
    -DNO_TESTS=ON \
    -DNO_GLTESTS=ON \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DDXSDK_ROOT=${MINGW_PREFIX} \
    -DTBB_LIBRARIES=${MINGW_PREFIX}/lib/libtbb12.dll.a \
    ../OpenSubdiv-${pkgver//./_}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .
}
